 #include "head.h"

/*函数声明*/
void do_stuff_query(int sockfd, MSG *pbuf);
void do_admin_del(int sockfd, MSG *pbuf);
void do_admin_add(int sockfd, MSG *pbuf);
void do_change_passwd(int sockfd, MSG *pbuf);


/**
 ** @author:	  王辰
 ** @date:        2016.12.27
 ** @brief:       登录
 */
int do_login(int sockfd, MSG *pbuf){
	int a;
	
LOOP:
	pbuf->type = LOGIN;
	puts("==================client login===================");
	puts("1、Login		2、Exit");
	
	scanf("%d",&a);
	getchar();
	
	if (a == 1) {

		puts("please input your name:");
		gets(pbuf->name);

		puts("please input your password:");
		gets(pbuf->passwd);
	
		send(sockfd, pbuf, sizeof(MSG), 0);
		recv(sockfd, pbuf, sizeof(MSG), 0);

		if (pbuf->type == FAILED) {
		
			printf("name or password err!");
			goto LOOP;
			
		} else if(pbuf->type == SUCCESS){
		
			return pbuf->ret;
		} else {
			printf("err! try again!");
			goto LOOP;
		}		
	} else if (a == 2) {
		close(sockfd);
		exit(0);
	} else {
			printf("err! try again!");
			goto LOOP;
	}
}

/**
 ** @date:        2016.12.27
 ** @brief:       员工界面
 */
void do_stuff(int sockfd, MSG *pbuf){
	int a;

	for(;;){
		puts("==================please choise===================");
		puts("1、Change password	2、Search user	3、Exit");

		scanf("%d",&a);

		switch (a) {
		case 1:
			pbuf->ret = STAFF;
			do_change_passwd(sockfd,pbuf);
			break;
		case 2:
			do_stuff_query(sockfd,pbuf);
			break;
		case 3:
			close(sockfd);
			exit(0);
		}
	}
}

/**
 ** @author:	  蒯进
 ** @date:        2016.12.27
 ** @brief:       员工查询
 */
void do_stuff_query(int sockfd, MSG *pbuf){
	char name[16];

	pbuf->type = STAFF_QUERY;
		getchar();
		printf("input the name\n");
		gets(name);

		if(strcmp(pbuf->name,name) != 0){
			puts("name is wrong");
			return;
		}

		if(strcmp(pbuf->info.name,"#") == 0){
			return;
		}else{
			send(sockfd,pbuf,sizeof(MSG),0);
			recv(sockfd,pbuf, sizeof(MSG), 0);

			puts(pbuf->data);

			if(strcmp(pbuf->data,"ok") == 0){
			printf("name:%s ,id:%d ,addr:%s ,age:%d ,salary:%lf\n",pbuf->info.name,pbuf->info.id,pbuf->info.addr,pbuf->info.age,pbuf->info.salary);
			}

			printf("\n");
		}
}

/**
 ** @date:        2016.12.27
 ** @brief:       管理员界面
 */
int do_admin(int sockfd, MSG *pbuf){
	int a;

LOOP1:	
    puts("==================please choise===================");
	puts("1、Add user	2、Delete user	3、Change password");
	puts("4、Search user	5、Exit");

	scanf("%d",&a);
	getchar();
	
	switch (a) {
	case 1:
		do_admin_add(sockfd,pbuf);
		goto LOOP1;
		break;
	case 2:
		do_admin_del(sockfd,pbuf);
		goto LOOP1;
		break;
	case 3:
		pbuf->ret = ADMIN;
		do_change_passwd(sockfd,pbuf);
		goto LOOP1;
		break;	
	case 4:
		do_admin_query(sockfd,pbuf);
		goto LOOP1;
		break;	
	case 5:
		close(sockfd);
		exit(0);
		break;
	}

	return 0;
}

/**
 ** @author:	  蒯进
 ** @date:        2016.12.27
 ** @brief:       管理员查询
 */
int do_admin_query(int sockfd, MSG *pbuf){
	pbuf->type = ADMIN_QUERY;
	printf("input the name\n");
	gets(pbuf->info.name);

	if(strcmp(pbuf->info.name,"#") == 0)
	{
		return -1;
	}else{
		send(sockfd,pbuf,sizeof(MSG),0);
		recv(sockfd, pbuf,sizeof(MSG), 0);
		puts(pbuf->data);

		if(strcmp(pbuf->data,"ok") == 0){
			printf("name:%s ,id:%d ,addr:%s ,age:%d ,salary:%lf\n",pbuf->info.name,pbuf->info.id,pbuf->info.addr,pbuf->info.age,pbuf->info.salary);
		}

		printf("\n");
	}

	return 0;
}

/**
 ** @author:	  孔祥锴
 ** @date:        2016.12.27
 ** @brief:       管理员增加
 */
void do_admin_add(int sockfd,MSG *pbuf){
	pbuf->type = ADMIN_ADD_STAFF;
	printf("input the name\n");
	gets(pbuf->info.name);

	if(strcmp(pbuf->info.name,"#") == 0){
		printf("quit delete\n");
		return;
	}

	printf("input the password\n");
	gets(pbuf->passwd);

	printf("input the id \n");                                                                                                                                
	scanf("%d",&pbuf->info.id);
	getchar();

	if(pbuf->info.id == 0){
		printf("quit delete\n");
		return;
	}        

	printf("input the address \n");                                                                                                                                
	gets(pbuf->info.addr);

	if(strcmp(pbuf->info.addr,"#") == 0){
		printf("quit delete\n");
		return;
	}        

	printf("input the salary \n");                                                                                                                                
	scanf("%f",&pbuf->info.salary);
	getchar();

	if(pbuf->info.salary == 0){
		printf("quit delete\n");
		return;
	}        

	printf("input the age \n");                                                                                                                                
	scanf("%d",&pbuf->info.age);
	getchar();

	if(pbuf->info.age  == 0){
		printf("quit delete\n");
		return;
	}        

	send(sockfd,pbuf,sizeof(MSG),0);                                                                                                                      
	recv(sockfd,pbuf,sizeof(MSG),0);

	printf("%s\n",pbuf->data);
	printf("\n");

}

/**
 ** @author:	  孔祥锴
 ** @date:        2016.12.27
 ** @brief:       管理员删除
 */
void do_admin_del(int sockfd, MSG *pbuf){
	pbuf->type = ADMIN_DEL_STAFF;
	printf("delete user by name and id\n");
		
	printf("input the name\n");		
	gets(pbuf->info.name);

	if(strcmp(pbuf->info.name,"#") == 0){
		printf("quit delete\n");
		return;
	}

	printf("input the id \n");
	scanf("%d",&pbuf->info.id);
	getchar();
	
	if(pbuf->info.id == 0){
		printf("quit delete\n");
		return;
	}else{
		send(sockfd,pbuf,sizeof(MSG),0);
		recv(sockfd,pbuf,sizeof(MSG),0);
		printf("%s\n",pbuf->data);
		printf("\n");
	}
}

/**
 * @brief 客户端admin，staff调用同一个函数修改staff密码，输入两次密码，相同的话，发送给服务器端。
 *        然后等待接收服务器端发送的修改成功返回值。
 * @param 
 * @return 
 */ 
 /**
 ** @author:	  王陈陈
 ** @date:        2016.12.27
 ** @brief:       管理员，员工修改密码
 */
void do_change_passwd(int sockfd, MSG *pbuf){
	char passwd_new_1[64];
	char passwd_new_2[64];

	int nbytes;

//	getchar(); //这里加getchar()之后，发现在前面case3之后，不会直接执行puts();而是要再敲下enter，才能执行puts();
            	//注意:getchar()接收行缓存，以\n结尾，遇\n结束，吸收\n,scanf不吸收\n，因此，
	          //如果scanf后面紧跟gets()函数的话，会直接跳过gets()，

	pbuf->type = STAFF_CHANGE_PASSWD;
	send(sockfd,pbuf,sizeof(MSG),0);

	puts("");
	puts("-----------ready to input-----------");

	for(;;){		
		puts("please input new passwd,less than 64 bits:");
	//	getchar();	//莫名其妙，这里竟然输入123，结果更改后的密码是23，可见1是被getchar掉了
		gets(passwd_new_1);
		
		//这里可以用strlen判断64位的问题。


		puts("please input new passwd again,less than 64 bits:");
	//	getchar();
		gets(passwd_new_2);

		if(strcmp(passwd_new_1,passwd_new_2) != 0){
			puts("two new different passwds"); //puts自带换行
		}else{
			break;
		}
	}

	strcpy(pbuf->passwd,passwd_new_2);
	nbytes=send(sockfd, pbuf, sizeof(MSG), 0);

	if(nbytes < 0){
		perror("send new passwd err\n");
		exit(-1);
	}else{
		printf("send new passwd success\n");
	}

	recv(sockfd, pbuf, sizeof(MSG), 0);

	if(pbuf->ret == SUCCESS){
		printf("update passwd success\n");
	}else{
		printf("update passwd fail\n");
		exit(-1);
	}
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }

    ret = do_login(sockfd, &buf);
    if(ret == STAFF){  //员工
        puts("stuff login");
        do_stuff(sockfd, &buf);
    }else{  //管理员
        puts("admin login");
        do_admin(sockfd, &buf);
    }
}
